package com.mall.controller;

import com.alibaba.fastjson.JSON;
import com.mall.bean.SystemRolePermission;
import com.mall.bean.SystemUserRole;
import com.mall.service.SystemUserRoleService;
import com.mall.vo.NodeVo;
import com.mall.vo.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;
import java.util.UUID;

/**
 * @PageName com.mall.controller
 * @Description 用户角色
 * @Author 何全裕
 * @Create 2017-09-09 19:28
 * @Version 1.0
 **/
@Controller
@RequestMapping("user_role")
public class SystemUserRoleController {
    @Autowired
    private SystemUserRoleService systemUserRoleService;

    @RequestMapping("/getSystemUserRole")
    public ModelAndView getSystemUserRole(String user_id){
        ModelAndView modelAndView = new ModelAndView("/system_user/SystemUserRole");
        List<NodeVo> role = systemUserRoleService.getSyetemUserRole(user_id);
        modelAndView.addObject("role", JSON.toJSON(role));
        return modelAndView;
    }


    @ResponseBody
    @RequestMapping("/deleteUserRoleByUserId")
    public Response deleteUserRoleByUserId(String user_id){
        systemUserRoleService.deleteUserRoleByUserId(user_id);
        return new Response("0","","");
    }


    @ResponseBody
    @RequestMapping("/deleteUserRoleByRoleId")
    public Response deleteUserRoleByRoleId(String role_id){
        systemUserRoleService.deleteUserRoleByRoleId(role_id);
        return new Response("0","","");
    }

    @ResponseBody
    @RequestMapping("/insertUserRole")
    public Response insertUserRole(SystemUserRole user_role, String menuIds){
        systemUserRoleService.deleteUserRoleByUserId(user_role.getSysUserId());
        if(menuIds == null || menuIds.equals("")){
            return new Response("0","","");
        }
        String[] p_ids = menuIds.split(";");
        for(String pid : p_ids){
            user_role.setId(UUID.randomUUID().toString());
            user_role.setSysRoleId(pid);
            systemUserRoleService.insertUserRole(user_role);
        }
        return new Response("0","","");
    }

}
